import {getClassImageList} from "../../../api/moretap"
import {useStore} from "vuex";
import {onMounted,reactive,ref} from "vue"
export function useClassImageList(){
	const BASE_URL = import.meta.env.VITE_BASE_API
	const {state} = useStore()
	const DAT = ref<any>([])
	const COUNT = ref<number>(0)
	function initClassImageList(params:{
		ClassName:string
		Page:string
		PageSize:string
	}){
		return new Promise((resolve,reject)=>{
			getClassImageList({
				CLASSNAME:params?.ClassName,
				PAGEINDEX:params?.Page  || "1",
				PAGESIZE:params?.PageSize || "10",
				OPERATOR_ID:state.user.userInfo.CUSER_ID
			})
			.then(res=>{
				if (res.ERROR[0].errcode == '0'){
					let newDAT = []
					for(let i =0;i<res.DAT.length;i++){
						res.DAT[i].wx_pic = res.DAT[i].WX_PIC
						res.DAT[i].WX_PIC = BASE_URL + "/pic_server/browse?picfilename=" + res.DAT[i].WX_PIC
						res.DAT[i].hide = true
						newDAT.push(res.DAT[i])
					}
					DAT.value = newDAT
					COUNT.value = parseInt(String(res.COUNT[0].NUM / 10)) + (res.COUNT[0].NUM % 10 === 0 ? 0 : 1)
					resolve(res)
				}else {
					uni.showToast({
						title: res.ERROR[0].error,
						icon: 'none',
						mask: true
					})
					reject('err')
				}
			})
			.catch(err=>{
				uni.showToast({
					title: '获取列表失败',
					icon: 'error',
					mask: true
				})
				reject('err')
			})
		})
	}
	return {
		initClassImageList,
		DAT,
		COUNT
	}
}